package com.weixin.util;
 

import org.json.JSONObject;

import com.new2i.util.HttpConnectionUtil; 


/**
 * 微信接口Http请求工具类
 * 
 * @since 2015/08/26
 * @author 2i
 *
 */
public class WeixinHttpUtil {  
	
	
	//公众号AppID
	private final static String WX_APP_ID = "wx9e5539483762e8be";
//	private final static String WX_APP_ID = "wx4d3d99c6e3361c09";

	//公众号AppSecret
	private final static String WX_APP_SECRET = "9ecbfea0441f5c2af194b63b94e0ddd5";
//	private final static String WX_APP_SECRET = "2b6fb01cb03f4c431cfa083a571fd31c";
	 
	//{method:GET}
	private final static String WX_INTERFACE_TOKEN = "https://api.weixin.qq.com/cgi-bin/token"; 

	//{method:GET},{param:openid,lang}
	private final static String WX_INTERFACE_USER_INFO = "https://api.weixin.qq.com/cgi-bin/user/info"; 

	//{method:GET}
	private final static String WX_INTERFACE_OAUTH2_TOKEN = "https://api.weixin.qq.com/sns/oauth2/access_token"; 
	
	private static long TIMESTAMP = 0;
	
	private static String WX_ACCESS_TOKEN = null;
	
	public final static String WX_MESSAGE_TYPE_TEXT = "text";

	public final static String WX_MESSAGE_TYPE_EVENT = "event";

	public final static String WX_MESSAGE_TYPE_LOCATION = "location";

	public final static String WX_MESSAGE_TYPE_VOICE = "voice";
	
	public final static int WX_PLATFROM_NUM = 3;
	
	public final static int MCH_DOCTOR_ID = 10960;
	
	//静态内部类，保证http对象单例
	private static class LazyHolder {
		private static final WeixinHttpUtil http = new WeixinHttpUtil();
	}

	private WeixinHttpUtil() {}

	public static final WeixinHttpUtil getInstance() {
		return LazyHolder.http;
	} 
	
	/**
	 * 获取微信token,如果token过时,则向微信服务器刷新一次
	 * 
	 * @author 2i
	 * @since 2015/08/26
	 */
	public void getWeixinAcessToken(){ 
		long currentTime = System.currentTimeMillis();
		if(WX_ACCESS_TOKEN == null || currentTime - TIMESTAMP > 3600){ 
			TIMESTAMP = System.currentTimeMillis(); 
			String param = "grant_type=client_credential&appid="+WX_APP_ID+"&secret="+WX_APP_SECRET;
			String responseStr = HttpConnectionUtil.sendGet(WX_INTERFACE_TOKEN, param);
			try { 
				JSONObject jsonObj = new JSONObject(responseStr);
				System.out.println(responseStr);  
				if(jsonObj.has("access_token")){
					WX_ACCESS_TOKEN = jsonObj.getString("access_token");
				} 
			}catch (Exception e) {
				System.out.println(e.getLocalizedMessage());
			} 
		}  
		System.out.println("WX_ACCESS_TOKEN ==> "+WX_ACCESS_TOKEN);  
	} 
	
	/**
	 * 根据微信OpenId向微信查询用户信息
	 * 
	 * @author 2i
	 * @since 2015/08/27
	 * @param wxid
	 * @return
	 */
	public String getWxUserInfoByWxid(String wxid){ 
		getWeixinAcessToken(); 
		String params = "access_token=" + WX_ACCESS_TOKEN + "&openid=" + wxid + "&lang=zh_CN";
		String responseStr = HttpConnectionUtil.sendGet(WX_INTERFACE_USER_INFO, params); 
		return responseStr; 
	}
	
	/**
	 * 通过code获取access_token进行Oauth2授权
	 * 
	 * @author 2i
	 * @since 2015/08/27
	 * @param code
	 * @return
	 */
	public String getWxidByOauth2Code(String code) {
		if(code == null || code.length() <= 0){
			return null;
		} 
		String wxid = "";
		String params = "appid=" + WX_APP_ID +"&secret="+WX_APP_SECRET+"&code="+code+"&grant_type=authorization_code"; 
		String responseStr = HttpConnectionUtil.sendGet(WX_INTERFACE_OAUTH2_TOKEN, params);  
		System.out.println(responseStr);   
		try { 
			JSONObject jsonObj = new JSONObject(responseStr); 
			if(!jsonObj.has("errcode")){
				wxid = jsonObj.getString("openid");
				System.out.println("wxid===>" + wxid); 
			} 
		}catch (Exception e) {
			System.out.println(e.getLocalizedMessage());
		}   
		return wxid; 
	}
}
